﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using Lawyer.Model;
namespace Lawyer.BLL
{
	/// <summary>
	/// FileInfo
	/// </summary>
	public partial class FileInfo
	{
		private readonly Lawyer.DAL.FileInfo dal=new Lawyer.DAL.FileInfo();
		public FileInfo()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string FileID)
		{
			return dal.Exists(FileID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Lawyer.Model.FileInfo model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Lawyer.Model.FileInfo model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string FileID)
		{
			
			return dal.Delete(FileID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string FileIDlist )
		{
			return dal.DeleteList(FileIDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Lawyer.Model.FileInfo GetModel(string FileID)
		{
			
			return dal.GetModel(FileID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Lawyer.Model.FileInfo GetModelByCache(string FileID)
		{
			
			string CacheKey = "FileInfoModel-" + FileID;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(FileID);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Lawyer.Model.FileInfo)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}

        public void DeleteFiles(string FileIDlist)
        {
            DataSet pathList = dal.GetPathList(FileIDlist);
            DataTable dt = pathList.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                System.IO.FileInfo file = new System.IO.FileInfo(dt.Rows[i][0].ToString());
                if (file.Exists)
                {
                    file.Delete();
                }
            }
        }
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Lawyer.Model.FileInfo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Lawyer.Model.FileInfo> DataTableToList(DataTable dt)
		{
			List<Lawyer.Model.FileInfo> modelList = new List<Lawyer.Model.FileInfo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Lawyer.Model.FileInfo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Lawyer.Model.FileInfo();
					if(dt.Rows[n]["FileID"]!=null && dt.Rows[n]["FileID"].ToString()!="")
					{
					model.FileID=dt.Rows[n]["FileID"].ToString();
					}
					if(dt.Rows[n]["FileName"]!=null && dt.Rows[n]["FileName"].ToString()!="")
					{
					model.FileName=dt.Rows[n]["FileName"].ToString();
					}
					if(dt.Rows[n]["FileNote"]!=null && dt.Rows[n]["FileNote"].ToString()!="")
					{
					model.FileNote=dt.Rows[n]["FileNote"].ToString();
					}
					if(dt.Rows[n]["FileURL"]!=null && dt.Rows[n]["FileURL"].ToString()!="")
					{
					model.FileURL=dt.Rows[n]["FileURL"].ToString();
					}
					if(dt.Rows[n]["LawyerID"]!=null && dt.Rows[n]["LawyerID"].ToString()!="")
					{
					model.LawyerID=dt.Rows[n]["LawyerID"].ToString();
					}
					if(dt.Rows[n]["ArchiveID"]!=null && dt.Rows[n]["ArchiveID"].ToString()!="")
					{
					model.ArchiveID=dt.Rows[n]["ArchiveID"].ToString();
					}
					if(dt.Rows[n]["IsArchive"]!=null && dt.Rows[n]["IsArchive"].ToString()!="")
					{
						model.IsArchive=int.Parse(dt.Rows[n]["IsArchive"].ToString());
					}
					if(dt.Rows[n]["CreatedBy"]!=null && dt.Rows[n]["CreatedBy"].ToString()!="")
					{
					model.CreatedBy=dt.Rows[n]["CreatedBy"].ToString();
					}
					if(dt.Rows[n]["CreatedDate"]!=null && dt.Rows[n]["CreatedDate"].ToString()!="")
					{
						model.CreatedDate=DateTime.Parse(dt.Rows[n]["CreatedDate"].ToString());
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

